package com.mazaiting.redeye.base.module

import android.content.SharedPreferences
import android.preference.PreferenceManager

import com.mazaiting.easy.app.BaseApplication
import com.mazaiting.easy.utils.rx.RxUtil
import com.mazaiting.redeye.base.Config
import com.mazaiting.redeye.base.interceptors.RequestInterceptor

import dagger.Module
import dagger.Provides
import retrofit2.Retrofit
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory
import retrofit2.converter.gson.GsonConverterFactory

/**
 * 网络模块
 */
@Module
class NetModule {
    /**
     * 提供SharedPreference 存储对象
     * @param application 全局Application
     * @return SharedPreferences存储对象
     */
    @Provides
    internal fun providedSharedPreferences(application: BaseApplication): SharedPreferences
        = PreferenceManager.getDefaultSharedPreferences(application)

    /**
     * 获取Retrofit.Builder对象
     * @param application 全局Application对象
     * @return Retrofit.Builder对象
     */
    @Provides
    internal fun providedRetrofitBuilder(application: BaseApplication): Retrofit.Builder
        = Retrofit.Builder()
            // 添加Gson解析
            .addConverterFactory(GsonConverterFactory.create())
            // 添加RxJava适配器
            .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
            // 设置OkHttpClient
            .client(
//                RxUtil.getOkHttpClientWithInterceptor(application, arrayOf(RequestInterceptor())))
                RxUtil.getOkHttpClientWithInterceptor(application, emptyArray()))

    @Provides
    internal fun providedRetrofit(builder: Retrofit.Builder): Retrofit = builder.baseUrl(Config.URL_BASE).build()
}
